﻿using Abp.Dependency;
using Abp.Domain.Repositories;
using Batman.Cache;
using Batman.Cache.Attributes;
using Batman.Cms.Core.Const;
using Batman.Core;
using Batman.Jianbei.Forum.Core.ChatRoom;
using Batman.Jianbei.Forum.Core.Datas;
using Batman.Jianbei.Forum.Services.Users;
using Microsoft.EntityFrameworkCore;
using System.Collections.Generic;
using System.Linq;

namespace Batman.Jianbei.Forum.Services.ChatRoom.CacheQs
{
    public class RoomCacheQs : ICacheWorkable, ISingletonDependency
    {
        public IRepository<TalkRoom, string> TalkRoomRepo { get; set; }

        public RoleCacheService RoleCacheService { get; set; }

        /// <summary>
        /// 有效的聊天室列表
        /// </summary>
        [Cacher(CacherType.Cache)]
        public List<TalkRoom> GetRoomList()
        {
            var query = TalkRoomRepo.GetQuery().Where(t => !t.IsDismiss);
            var rooms = query.OrderByDescending(t => t.CreateAt).ToList();

            return rooms;
        }


        /// <summary>
        /// 获取由管理员或者超级管理员创建的(未解散的)房间编号
        /// </summary>
        [CacherAttribute(Id = CacheNames.AdminRoomIds)]
        public virtual List<string> GetAdministratorsRoomIds()
        {

            var admins = RoleCacheService.GetAdminOrSupperAdminUids();

            using (var ctx = new ChatRoomDbFactory().CreateDbContext(null))
            {
                var query = ctx.TalkRoom.AsNoTracking().Where(t => admins.Contains(t.CreateBy) && !t.IsDismiss);
                var result = query.Select(t => t.Id).ToList();
                return result;
            }

        }
    }
}
